﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using TDB;

namespace TDB.Client
{
  internal class TypeResolver : DataContractResolver
  {
    public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
    {
      if (typeof(ITypeStore).IsAssignableFrom(dataContractType))
      {
        XmlDictionary dictionary = new XmlDictionary();
        typeName = dictionary.Add(dataContractType.FullName);
        typeNamespace = dictionary.Add("http://teamdev.it/tdb");
        return true;
      }
      else
      {
        return knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace);
      }
    }

    public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
    {
      return Type.GetType(typeName);
    }
  }
}
